package com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.entity.LoginInfEntity;
import com.example.win.android_banduyujia.entity.OpenEntity;
import com.example.win.android_banduyujia.http.request.AsycHttpRequest;
import com.example.win.android_banduyujia.http.request.GetUserInfoRequest;
import com.example.win.android_banduyujia.http.response.BaseHttpResponse;
import com.example.win.android_banduyujia.login.application.ILoginStatusChangedListener;
import com.example.win.android_banduyujia.login.application.LoginApplication;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.MateralActivity;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.TarActivity;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.firmactivity.MyfirmActivity;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity.DataActivity.DataActivity;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity.courseactivity.activity.CourseActivity;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity.courseactivity.activity.TodayActivity;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity.orderactivity.MyOrderActivity;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.payactivtiy.MyincomeActivity;
import com.example.win.android_banduyujia.peopleactivity.ui.activity.BaseActivity;
import com.example.win.android_banduyujia.ui.view.CircleImageView;
import com.example.win.android_banduyujia.ui.view.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Win on 2015/8/13.
 */
public class OpenActivity extends Activity implements View.OnClickListener, ILoginStatusChangedListener{
   public static final String  TAG=OpenActivity.class.getSimpleName();

   private static  final  String  CircleImageView = null;
   private CircleImageView  mCircleImageView;

   private ImageView mIageNotifition,mIageSettings;  // 通知消息，设置
   private TextView  mTvtName,mTvtNumber; // 名字,进度条数字
   private ImageView mIgeIcon; // 图标
   private RatingBar mRatBar; // 星星进度条
   private Button    mBtnData; // 完善资料
   private TextView  mTvtCourse, mTvtToday; // 已发课数， 今日课程
   private Button    mBtnCourse, mBtnToday;
   private ImageView mIgeOrder, mIgeIncome, mIgeCourse; // 今日订单，我的收入，我的课程
   private TextView  mTvtOrder, mTvtIncome, mTvtMeCourse;
   private TextView  mTvtTar, mTvtComtar; // 发布私教， 企业课

   private RelativeLayout   LayoutNotification,LayoutLCS;

   private List<OpenEntity>   list = new ArrayList<OpenEntity>();

   private String    mAccessToken;
   private BaseActivity     mContext;
   private AsycHttpRequest  mAsyncHttpRequest;
   private LoginApplication mLoginAppLication;

    // 关于请求用户头像的变量
   private final int REFRESH_ICON = 1;
   private Bitmap  bitmapIcon = null;
   // 刷新头像
   private Handler handler = new Handler(){
       @Override
       public void handleMessage(Message msg) {
           switch (msg.what){
               case REFRESH_ICON:
                   mCircleImageView.setImageBitmap(bitmapIcon);
                   break;
           }
       }
   };

    public OpenActivity(){
         super();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_page_people);

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

       InitViews();
       refreshViews();
       addLoginListener();
    }

    private void addLoginListener() {
        mLoginAppLication = LoginApplication.getInstance();
        mLoginAppLication.addLoginOrLogoutListener(this);
    }

    private void InitViews() {
        LayoutNotification=(RelativeLayout)findViewById(R.id.layout_linear_page);
        mIageNotifition = (ImageView) findViewById(R.id.layout_page_image_view_notification);
        mIageSettings = (ImageView) findViewById(R.id.layout_page_image_view_settings);
        mCircleImageView = (CircleImageView) findViewById(R.id.me_fragment_header_view);
        mTvtName = (TextView) findViewById(R.id.me_fragment_text_view);
        mIgeIcon = (ImageView) findViewById(R.id.me_fragment_image_icon);
        mRatBar = (RatingBar) findViewById(R.id.rating_bar);
        mTvtNumber = (TextView) findViewById(R.id.layout_people_text_view_number);
        mBtnData = (Button) findViewById(R.id.me_fragment_btn_data);

        LayoutLCS=(RelativeLayout)findViewById(R.id.me_fragment_ralative_layout);
        mTvtToday = (TextView) findViewById(R.id.me_fragemnt_text_today_course_number);
        mTvtCourse = (TextView) findViewById(R.id.me_fragemnt_text_today_course);
        mBtnCourse = (Button) findViewById(R.id.me_fragment_button_today_number);
        mBtnToday = (Button) findViewById(R.id.me_fragment_btn_today_number);

        mIgeOrder = (ImageView) findViewById(R.id.me_fragemnt_image_order);
        mIgeIncome = (ImageView) findViewById(R.id.me_fragemnt_image_income);
        mIgeCourse = (ImageView) findViewById(R.id.me_fragemnt_image_course);

        mTvtOrder = (TextView) findViewById(R.id.me_fragemnt_text_order_my);
        mTvtIncome = (TextView) findViewById(R.id.me_fragemnt_text_course_my);
        mTvtMeCourse = (TextView) findViewById(R.id.me_fragemnt_text_couser_my);
        mTvtTar = (TextView) findViewById(R.id.me_fragment_text_view_release_tar);
       // mTvtComtar = (TextView) findViewById(R.id.me_fragment_text_view_release_com_tar);

        mIageNotifition.setOnClickListener(this);
        mIageSettings.setOnClickListener(this);
        mCircleImageView.setOnClickListener(this);
        mBtnData.setOnClickListener(this);
        mTvtToday.setOnClickListener(this);
        mTvtCourse.setOnClickListener(this);
        mIgeOrder.setOnClickListener(this);
        mIgeIncome.setOnClickListener(this);
        mIgeCourse.setOnClickListener(this);
        mTvtTar.setOnClickListener(this);
      //  mTvtComtar.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_page_image_view_notification: // 通知消息
                startActivity(new Intent(this, NotificationActivity.class));
                break;
            case R.id.layout_page_image_view_settings: // 设置
                 SetActivity();
                break;
            case R.id.me_fragment_header_view: // 头像
                PhoneActivity();
                break;
            case R.id.me_fragment_btn_data: // 完善资料
                DatoActivity();
                break;
            case R.id.me_fragemnt_text_today_course_number: // 今日课程数
                todayActivity();
                break;
            case R.id.me_fragemnt_text_today_course:   // 已发课程
                courseActivity();
                break;
            case R.id.me_fragemnt_image_order: // 我的订单
                MyOrderActivity();
                break;
            case R.id.me_fragemnt_image_income: // 我的收入
                MyIncomeActivity();
                break;
            case R.id.me_fragemnt_image_course: // 我的课程
                 MyProActivity();
                break;
            case R.id.me_fragment_text_view_release_tar: // 发布私教
                 TarActivity();
                break;
            /*case R.id.me_fragment_text_view_release_com_tar: // 发布企业课
                TarComActivity();
                break;*/
        }
    }

    private void PhoneActivity() {
        startActivity(new Intent(this, DataActivity.class));
    }

    private void courseActivity() {
        startActivity(new Intent(this, CourseActivity.class));
    }

    private void todayActivity() {
        startActivity(new Intent(this, TodayActivity.class));
    }

    private void SetActivity() {
        startActivity(new Intent(this, SettingsActivity.class));
    }

    private void MyOrderActivity() {
         startActivity(new Intent(this, MyOrderActivity.class));
    }

    private void MyIncomeActivity() {
         startActivity(new Intent(this, MyincomeActivity.class));
    }

    private void DatoActivity() {  // 完善资料
        startActivity(new Intent(this, MateralActivity.class));
    }

    private void MyProActivity() {
        startActivity(new Intent(this, MyProjectActivity.class));
    }

    private void TarComActivity() {  // 发布企业课
         startActivity(new Intent(this, MyfirmActivity.class));
    }

    private void TarActivity() {  // 发布私教
        startActivity(new Intent(this, TarActivity.class));
    }

    @Override
    public void isLoginOrLogout() {
        refreshViews();
    }

    protected void refreshViews() {
       if(LoginApplication.getInstance().isLogin()){
           LoginInfEntity  loginInfo = LoginApplication.getInstance().getUser();
           try{
              getUserInfo();
           }catch (Exception e){
               e.printStackTrace();
           }

           if(loginInfo != null){

               ((TextView)LayoutLCS.findViewById(R.id.me_fragemnt_text_today_course)).setText("已发课程"+ loginInfo.getCurrser());
               ((TextView)LayoutLCS.findViewById(R.id.me_fragemnt_text_today_course_number)).setText("今日课程"+ loginInfo.getToday());
               ((ImageView)LayoutNotification.findViewById(R.id.layout_page_image_view_notification)).setTag(loginInfo.getNotification());

               // 头像问题
               Bitmap bit = ImageLoader.getInstance().getImage(loginInfo.getAvatarUrl());

               if(bit !=null){
                   mCircleImageView.setImageBitmap(bit);
               }else{
                   ImageLoader.getInstance().downloadImage(loginInfo.getAvatarUrl(), mCircleImageView);
               }
           }
       }
    }

    public void getUserInfo() {
        GetUserInfoRequest request = new GetUserInfoRequest(LoginApplication.getInstance().getAccessToken());
        mAsyncHttpRequest.doRequest((BaseActivity)mContext, request, BaseHttpResponse.class, mGetUserInfoHandler, false);
    }

    private Handler mGetUserInfoHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            LoginInfEntity loginInfo = (LoginInfEntity) msg.obj;
            loginInfo.setAccessToken(LoginApplication.getInstance().getAccessToken());
            mLoginAppLication.login(loginInfo);
           refreshViews();
        }
    };
}
